using UnityEngine;

public class RemotePlayer : PlayerBase
{
	public Transform racket;

	protected float racketY;

	protected Vector3 desiredPosition;

	protected float racketSpeed = 20f;

	protected GravityFlight serveFlight = new GravityFlight();

	public override void Init(int teamTag, int tableSide, MatchController match)
	{
		base.Init(teamTag, tableSide, match);
		racket.rotation = Quaternion.AngleAxis(-70f, Vector3.forward);
		racketY = Table.tabletopy + 0.125f;
		desiredPosition = new Vector3(0f, racketY, (float)tableSide * Table.halfLength);
	}

	public void SetPosition(MRemotePosInfo racketPos)
	{
		Vector3 vector = MultiplayerMatchController.RemoteToLocalPos(racketPos.playerPosition);
		Vector3 vector2 = MultiplayerMatchController.RemoteToLocalPos(racketPos.ballPos);
		PlayerBase player = base.match.GetPlayer(0);
		desiredPosition = vector;
		Ball ball = base.match.ball;
		if (ball.isBallInGame && ball.flightPlan.playerThatHitBall == player)
		{
			float num = ball.flightPlan.GetTotalDuration() * 0.5f;
			if (ball.flightPlan.reboundStage != null)
			{
				num = ball.flightPlan.GetDurationTillStage(ball.flightPlan.reboundStage);
			}
			desiredPosition = Vector3.Lerp(vector, vector - vector2 + ball.position, ball.flightPlan.flightTime / num);
		}
		desiredPosition.y = racketY;
	}

	public void HitBall(Ball ball, MHitBall hitBall)
	{
		base.isInServe = false;
		base.ball = ball;
		ball.position = hitBall.ballPosition;
		desiredPosition = new Vector3(hitBall.ballPosition.x, racket.position.y, hitBall.ballPosition.z);
		HitBall(hitBall.landingPosition, hitBall.heightAboveNet, hitBall.timeToLand, hitBall.spinx, hitBall.spiny, hitBall.isServe, hitBall.useNormalTime, hitBall.airResistance, hitBall.rebound, hitBall.racketTimeScale);
	}

	private void Update()
	{
		racket.position = Vector3.Lerp(racket.position, desiredPosition, Time.deltaTime * racketSpeed);
		Ball ball = base.match.ball;
		float num = ball.position.x - racket.position.x;
		racket.rotation = Quaternion.Lerp(racket.rotation, Quaternion.AngleAxis((float)MathEx.SignZeroPositive(num) * Mathf.Lerp(0f, 1f, 4f * Mathf.Abs(num) / Table.halfwidth) * -70f, Vector3.forward), Time.deltaTime * 20f);
		if (!(ball == null) && base.isInServe)
		{
			UpdateServeBall();
		}
	}

	private void UpdateServeBall()
	{
		Vector3 position = racket.position;
		position.x = Mathf.Clamp(position.x, 0f - Table.halfwidth, Table.halfwidth);
		position.z = (float)base.tableSide * Table.halfLength;
		serveFlight.timeOfFlight = Mathf.Repeat(serveFlight.timeOfFlight + Time.deltaTime, 2f * serveFlight.apexTime);
		position.y = serveFlight.Position(serveFlight.timeOfFlight).y;
		base.ball.position = position;
	}

	public override void BallRemovedFromGame()
	{
		base.BallRemovedFromGame();
		base.isInServe = false;
	}

	public override void StartServe(Ball ball)
	{
		Debug.Log("Remote Start Serve");
		base.ball = ball;
		ball.TakeBall();
		Vector3 position = racket.position;
		position.y = Table.tabletopy;
		position.x = Mathf.Clamp(position.x, 0f - Table.halfwidth, Table.halfwidth);
		ball.position = position;
		serveFlight.Fire(position, ParabolicFlight.initialVelocityForSimpleCase(position, position, Table.tabletopy + 0.3f), Physics.gravity);
		base.isInServe = true;
	}

	public override void ReceiveServe()
	{
		base.ReceiveServe();
	}

	public override void ReceiveBall(Ball ball)
	{
		base.ball = ball;
	}
}
